/*
 * Copyright (C) 2009 Kristopher T Babic
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, see <http://www.gnu.org/licenses>.
 */

package edu.idp.client;

import javax.imageio.ImageIO;
import java.applet.Applet;
import java.awt.*;
import java.net.URL;

/**
 *
 * @author Kristopher T Babic
 */
public class ToolPanel extends Panel {
    /**
     * @clientCardinality 1
     */
    private InterDrawPanel drawingPanel;
    private ToolButton toolSquare, toolEllipse, toolLine;
    private ToolButton[] tools;
    private ToolOptionPanel toolOptions;
    static private int NUM_TOOLS = 6;

    public ToolPanel(InterDrawPanel drawingPanel, Applet myApp) {
        tools = new ToolButton[NUM_TOOLS];
        this.drawingPanel = drawingPanel;

        URL base = myApp.getCodeBase();

        toolOptions = new ToolOptionPanel(drawingPanel);

/*
        try {
            tools[0] = new ToolButton(ImageIO.read(ToolPanel.class.getResource("/gifs/tool_square.gif")), "tool_rectangle", ToolOptionPanel.POLY);
            tools[1] = new ToolButton(ImageIO.read(ToolPanel.class.getResource("/gifs/tool_ellipse.gif")), "tool_ellipse", ToolOptionPanel.POLY);
            tools[2] = new ToolButton(ImageIO.read(ToolPanel.class.getResource("/gifs/tool_line.gif")), "tool_line", ToolOptionPanel.LINE);
            tools[3] = new ToolButton(ImageIO.read(ToolPanel.class.getResource("/gifs/tool_pen.gif")), "tool_pencil", ToolOptionPanel.NONE);
            tools[4] = new ToolButton(ImageIO.read(ToolPanel.class.getResource("/gifs/tool_airbrush.gif")), "tool_airbrush", ToolOptionPanel.AIRBRUSH);
            tools[5] = new ToolButton(ImageIO.read(ToolPanel.class.getResource("/gifs/tool_erase.gif")), "tool_eraser", ToolOptionPanel.NONE);
        }
        catch (Exception e) {
            throw new IllegalArgumentException("Images could not be located");
        }
*/
// converted to loading from classpath
        tools[0] = new ToolButton(myApp.getImage(base, "./gifs/tool_square.gif"), "tool_rectangle", ToolOptionPanel.POLY);
        tools[1] = new ToolButton(myApp.getImage(base, "./gifs/tool_ellipse.gif"), "tool_ellipse", ToolOptionPanel.POLY);
        tools[2] = new ToolButton(myApp.getImage(base, "./gifs/tool_line.gif"), "tool_line", ToolOptionPanel.LINE);
        tools[3] = new ToolButton(myApp.getImage(base, "./gifs/tool_pen.gif"), "tool_pencil", ToolOptionPanel.NONE);
        tools[4] = new ToolButton(myApp.getImage(base, "./gifs/tool_airbrush.gif"), "tool_airbrush", ToolOptionPanel.AIRBRUSH);
        tools[5] = new ToolButton(myApp.getImage(base, "./gifs/tool_erase.gif"), "tool_eraser", ToolOptionPanel.NONE);

        for (int i = 0; i < NUM_TOOLS; i++) {
            tools[i].addActionListener(this.drawingPanel);
            tools[i].addActionListener(this.toolOptions);
        }
/*
		tools[0].addMouseListener(new ToolButtonListener(new String("R"),this.drawingPanel, this.toolOptions));
		tools[1].addMouseListener(new ToolButtonListener(new String("E"),this.drawingPanel, this.toolOptions));
		tools[2].addMouseListener(new ToolButtonListener(new String("L"),this.drawingPanel, this.toolOptions));
		tools[3].addMouseListener(new ToolButtonListener(new String("F"),this.drawingPanel, this.toolOptions));
    tools[4].addMouseListener(new ToolButtonListener(new String("ER"),this.drawingPanel, this.toolOptions));
 */
        GridBagLayout gbl = new GridBagLayout();
        GridBagConstraints gbc = new GridBagConstraints();

        setLayout(gbl);

        int i;
        gbc.weightx = .5;
        for (i = 0; i < NUM_TOOLS; i++) {
            gbc.gridx = i % 2;
            gbc.gridy = i / 2;

            if (i % 2 == 0)
                gbc.anchor = GridBagConstraints.EAST;
            else
                gbc.anchor = GridBagConstraints.WEST;

            gbl.setConstraints(tools[i], gbc);
            add(tools[i]);
        }

        gbc.anchor = GridBagConstraints.CENTER;
        gbc.gridx = 0;
        if (i % 2 == 0)
            gbc.gridy = i / 2;
        else
            gbc.gridy = (i / 2) + 1;
        gbc.gridwidth = 2;
        gbc.weighty = 1.;


        gbl.setConstraints(toolOptions, gbc);
        add(toolOptions);
        tools[2].setDown();

        toolOptions.setOption(ToolOptionPanel.LINE);
    }

    public Dimension getPreferredSize() {
        return new Dimension(100, 500);
    }

    public Dimension getMinimumSize() {
        return new Dimension(100, 500);
    }

    public Insets getInsets() {
        return new Insets(5, 0, 5, 0);
    }
}

